package edu.fsu.cs.SHPE;
import java.net.ConnectException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;


public class ConferenceDisplayActivity extends Activity {

	private final static String TAG = "SHPE.ConferenceDisplayActivity";

	private VenueListAdapter venueAdapter;
	private SpeakerListAdapter speakersAdapter;
	private SessionListAdapter sessionAdapter;
	private int conferenceId;
	private ListView listView;
	private OnItemClickListener venueItemClickListener, speakersItemClickListener, sessionsItemClickListener;
	private Context context;
	private Spinner tracks;
	private ArrayAdapter<String> tracksAdapter;
	private OnItemSelectedListener tracksItemSelectedListener;
	private TracksContainer tracksContainer;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);

		setContentView(R.layout.conferencedisplay);
		setTitle("Conference Information");

		conferenceId = getIntent().getIntExtra("id", -1);

		if(conferenceId == -1){
			Toast.makeText(this, "Error. No such conference id (" + conferenceId + ")", Toast.LENGTH_LONG).show();
			finish();
		}


		tracks = (Spinner)findViewById(R.id.spnTracks);
 
		context = this;



		try {
			tracksContainer = new TracksContainer(Conference.insertId(getString(R.string.getTracks), conferenceId));

			for(String s : tracksContainer.getStrings())
				Log.i("Track String", s);


			tracksAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, tracksContainer.getStrings());
			tracksAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			tracksItemSelectedListener = new TracksItemSelectedListener();


			listView = (ListView) findViewById(R.id.listView);
			venueAdapter = new VenueListAdapter(this,Conference.insertId(getString(R.string.getVenus), conferenceId));
			venueItemClickListener = new VenueItemClickListener();


			sessionAdapter = new SessionListAdapter(this, Conference.insertId(getString(R.string.getSessions), conferenceId));
			sessionsItemClickListener = new SessionsItemClickListener();


			speakersAdapter = new SpeakerListAdapter(this, Conference.insertId(getString(R.string.getSpeakers), conferenceId));
			speakersItemClickListener = new SpeakersItemClickListener();


			listView.setAdapter(sessionAdapter);
			tracks.setAdapter(tracksAdapter);
			tracks.setVisibility(View.VISIBLE);
			tracks.setOnItemSelectedListener(tracksItemSelectedListener);
			listView.setOnItemClickListener(sessionsItemClickListener);


		} catch (ConnectException e) {
			Toast.makeText(this, "Unable to Connect", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}

	}


	public void onClick(View view){

		Log.i("onclick", "view id " + view.getId());

		if(view.getId() == R.id.btnSessions){
			listView.setAdapter(sessionAdapter);
			tracks.setVisibility(View.VISIBLE); 
			listView.setOnItemClickListener(sessionsItemClickListener);
		}
		else
			tracks.setVisibility(View.GONE);


		if(view.getId() == R.id.btnVenues){
			listView.setAdapter(venueAdapter);
			listView.setOnItemClickListener(venueItemClickListener);
		}

		if(view.getId() == R.id.btnSpeakers){
			listView.setAdapter(speakersAdapter);
			listView.setOnItemClickListener(speakersItemClickListener);
		}

	}

	private class TracksItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Log.i("track selected", "position " + position + ", id " + id);


			sessionAdapter.setViewableSessionsByTrackId(tracksContainer.get(position).getId());
			sessionAdapter.notifyDataSetChanged();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {


		}

	}

	private class SessionsItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Log.i(TAG, "Session Click position " + position + " id "+ id);


			//Session.addToCalendar(context, "test", 1, 1);

			Intent intent = new Intent(context,SessionActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("id", sessionAdapter.getItemId(position));

			context.startActivity(intent);

		}

	}


	private class SpeakersItemClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			final Speaker tmpSpeaker = (Speaker) speakersAdapter.getItem(position);

			final CharSequence[] options = {"Email", "Website", "LinkedIn", "Read Bio"};

			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle("What do you want to do?");
			dialog.setItems(options, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent();

					switch(which){
					case 0:
						if(tmpSpeaker.getEmail() != null){
							intent.putExtra(Intent.EXTRA_EMAIL, new String[]{tmpSpeaker.getEmail()});
							intent.setAction(Intent.ACTION_EDIT);
							intent.setType("plain/text");
						}
						break;

					case 1:
						if(tmpSpeaker.getWebsite() != null){
							intent.setData(Uri.parse(tmpSpeaker.getWebsite()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					case 2:
						if(tmpSpeaker.getLinkedIn() != null){
							intent.setData(Uri.parse(tmpSpeaker.getLinkedIn()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					case 3:
						intent.setClass(context, SpeakerActivity.class);
						intent.setAction(Intent.ACTION_VIEW);
						intent.putExtra("id", tmpSpeaker.getId());
						break;
					}

					if(intent.getAction() != null)
						context.startActivity(intent);

				}
			});

			AlertDialog alert;

			if(tmpSpeaker != null){
				alert = dialog.create();
				alert.show();
			}


		}

	}

	private class VenueItemClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Log.i("venueclicklistener", "position " + position + ", id" + id);


			final Conference tmpConference = (Conference) venueAdapter.getItem(position);


			final CharSequence[] options = {"Call", "Map", "Website"};

			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle("What do you want to do?");
			dialog.setItems(options, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent();

					switch(which){
					case 0:
						if(tmpConference.getPhone() != null){
							intent.setData(Uri.parse("tel:" + tmpConference.getPhone()));
							intent.setAction(Intent.ACTION_DIAL);
						}
						break;
					case 1:
						if(tmpConference.getLat() != null && tmpConference.getLon() != null){ 
							intent = new Intent(context, VenuMapActivity.class);
							intent.putExtra("lat", Double.parseDouble(tmpConference.getLat()));
							intent.putExtra("lon", Double.parseDouble(tmpConference.getLon()));
							intent.putExtra("zoom", Integer.parseInt(context.getString(R.string.zoomclose)));
							intent.putExtra("id", conferenceId);
							//intent.setData(Uri.parse("geo:" + tmpConference.getLat() + "," + tmpConference.getLon()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					case 2:
						if(tmpConference.getWebsite() != null){
							intent.setData(Uri.parse(tmpConference.getWebsite()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					default:
						intent = null;
						break;
					}

					if(intent != null){
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					}
					else
						Toast.makeText(context, "Option Not Available", Toast.LENGTH_SHORT).show();

				}
			});

			AlertDialog alert;

			if(tmpConference != null){
				alert = dialog.create();
				alert.show();
			}


		}

	}
}
